import java.io.*;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {

        System.out.println("ceshixinxi");
        ArrayList<Student> students = new ArrayList<>();

        read(students);

//        write(students);

        for (Student student : students) {
            int sum=student.getChinese()
                    +student.getMath()
                    +student.getEnglish()
                    +student.getPhysics()
                    +student.getChemistry()
                    +student.getBiology();
            System.out.println(student+" "+sum);
        }


        write(students);

    }

    public static void order(ArrayList<Student> students){
        int sum=0;
        int sum_1=0;
        Student student = new Student();
        for (int i=0;i< students.size()-1;i++){
            for (int j=i+1;j<students.size();j++){
                sum=students.get(i).getChinese()
                        +students.get(i).getMath()
                        +students.get(i).getEnglish()
                        +students.get(i).getPhysics()
                        +students.get(i).getChemistry()
                        +students.get(i).getBiology();
                sum_1=students.get(j).getChinese()
                        +students.get(j).getMath()
                        +students.get(j).getEnglish()
                        +students.get(j).getPhysics()
                        +students.get(j).getChemistry()
                        +students.get(j).getBiology();
                if (sum<sum_1){
                    student=students.get(i);
                    students.set(i,students.get(j));
                    students.set(j,student);
                }else if (sum==sum_1){
                    if (students.get(i).getId()>students.get(j).getId()){
                        student=students.get(i);
                        students.set(i,students.get(j));
                        students.set(j,student);
                    }
                }
            }
        }
    }

    public static void read(ArrayList<Student> students){
        try {

            //文件位置在盘根目录下
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\student.csv"), "utf-8"));//GBK
//            reader.readLine();
//            System.out.println(reader.readLine());
            String line = null;

            reader.readLine();//事先读取一行,吧表头干掉

            while((line=reader.readLine())!=null){
                String item[] = line.split(",");
                Student student = new Student();

                student.setId(Integer.parseInt(item[0]));
                student.setName(item[1]);
                student.setChinese(Integer.parseInt(item[2]));
                student.setMath(Integer.parseInt(item[3]));
                student.setEnglish(Integer.parseInt(item[4]));
                student.setPhysics(Integer.parseInt(item[5]));
                student.setChemistry(Integer.parseInt(item[6]));
                student.setBiology(Integer.parseInt(item[7]));
                students.add(student);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        order(students);
//        System.out.println("------------------------------------------");
//        for (Student student : students) {
//            int sum=student.getChinese()+ student.getMath()+student.getEnglish()+student.getPhysics()+student.getChemistry()+ student.getBiology();
//            System.out.println(student+"   "+sum);
//        }
    }



    public static void write(ArrayList<Student> students){
        try {
            File csvFile  = File.createTempFile("student", ".csv", new File("D:\\"));

            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), StandardCharsets.UTF_8), 1024);

            for (Student student1 : students) {
                bufferedWriter.write(student1.getId()+"");
                bufferedWriter.write(",");
                bufferedWriter.write(student1.getName()+"");
                bufferedWriter.write(",");
                bufferedWriter.write(student1.getChemistry()+"");
                bufferedWriter.write(",");
                bufferedWriter.write(student1.getMath()+"");
                bufferedWriter.write(",");
                bufferedWriter.write(student1.getEnglish()+"");
                bufferedWriter.write(",");
                bufferedWriter.write(student1.getPhysics()+"");
                bufferedWriter.write(",");
                bufferedWriter.write(student1.getChemistry()+"");
                bufferedWriter.write(",");
                bufferedWriter.write(student1.getBiology()+"");

                bufferedWriter.newLine();
            }
            bufferedWriter.flush();
            bufferedWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }








}

